package logic

import (
	"context"
	"microdemo/internal/model/usermodel"
	"microdemo/service/user/internal/svc"
	"microdemo/service/user/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddUserLogic {
	return &AddUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddUserLogic) AddUser(in *user.AddUserRequest) (*user.AddUserResp, error) {
	insert, err := l.svcCtx.UserModel.Insert(l.ctx, &usermodel.User{
		Name:   in.Name,
		Gender: in.Gender,
	})
	if err != nil {
		return nil, err
	}
	id, err := insert.LastInsertId()
	if err != nil {
		return nil, err
	}
	return &user.AddUserResp{
		Id: id,
	}, nil
}
